<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
	<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
	<title></title>
	<script type="text/javascript" src="../../js/boot.js"></script>
	<style>
		#calendar {
			width: 100%
		}

		.clearfix {
			margin-bottom: 10px !important;
		}
	</style>
</head>

<body>

	<div id="layout1" class="mini-layout" style="width:100%;height:100%;">
		<div title="组织人员" region="west" showProxyText="true" width="320" showSplitIcon="false">
			<div id="tabs1" class="mini-tabs" activeIndex="0" style="width:100%;height:98%;" plain="false">
				<div title="组织结构">
					<div class="mini-fit">
						<ul id="pageTree" class="mini-tree" style="width: 100%;" showTreeIcon="true" allowDrag="true"
							allowDrop="true" textField="departName" idField="departID" resultAsTree="false">
						</ul>
					</div>
				</div>
				<div title="员工列表">
					<!--员工列表-->
					<div class="mini-fit">
						<div id="userDatagrid" class="mini-datagrid" style="width:100%;height:98%;" pageSize="50"
							showReloadButton="false" showPageInfo="false">
							<div property="columns">
								<div width="6" type="indexcolumn"></div>
								<div width="6" type="checkcolumn"></div>
								<div width="30" field="workNo" headerAlign="center">工号</div>
								<div width="30" field="userName" headerAlign="center">姓名</div>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>

		<div title="center" region="center" id="content-wrapper">
			<div id="datagrid1" class="mini-datagrid" showPager="false" style="width:100%;height:100%;"
				multiSelect="true" allowCellSelect="true">
				<div property="columns">
					<div field="0" headerAlign="center">周一</div>
					<div field="1" headerAlign="center">周二</div>
					<div field="2" headerAlign="center">周三</div>
					<div field="3" headerAlign="center">周四</div>
					<div field="4" headerAlign="center">周五</div>
					<div field="5" headerAlign="center">周六</div>
					<div field="6" headerAlign="center">周七</div>
				</div>
			</div>
		</div>
		<div title="编辑" region="east" width="230" showSplitIcon="false">
			<div>
				<center><a class="mini-button" iconCls="icon-save" onClick="createCalendar()">创建日历</a></center>
			</div>
			<div id="form1">
				<table>
					<tr>
						<td><label>设置年份：</label></td>
						<td><input id="year" name="year" class="mini-combobox" textField="text" valueField="id"
								data="YEARS" /></td>
					</tr>
				</table>
				<legend><span>设置节日</span></legend>
				<table>
					<tr>
						<td><label>选择日期：</label></td>
						<td><input name="calendarDate" class="mini-datepicker" /></td>
					</tr>
					<tr>
						<td><label>设置类型：</label></td>
						<td><input name="dayType" class="mini-combobox" textField="text" valueField="id"
								data="CONST.get('WorkDayType')" /></td>
					</tr>
					<tr>
						<td></td>
						<td>
							<a class="mini-button" iconCls="icon-save" onClick="saveFestval()">保存</a>
						</td>
					</tr>
				</table>
			</div>
			<br>
			<div id="form2">
				<legend><span>设置工作时间</span></legend>
				<table>
					<tr>
						<td><label>开始日期：</label></td>
						<td><input name="startDate" class="mini-datepicker" format="yyyy-MM-dd" /></td>
					</tr>
					<tr>
						<td><label>结束日期：</label></td>
						<td><input name="endDate" class="mini-datepicker" format="yyyy-MM-dd" /></td>
					</tr>
					<tr>
						<td><label>设置类型：</label></td>
						<td><input id="calendarSort" class="mini-combobox" textField="sortName" valueField="sortID" />
						</td>
					</tr>
					<tr>
						<td><label>上班时间：</label></td>
						<td><input id="startTime" name="startTime" class="mini-textbox" format="H:mm" enabled="false" />
						</td>
					</tr>
					<tr>
						<td><label>下班时间：</label></td>
						<td><input id="endTime" name="endTime" class="mini-textbox" format="H:mm" enabled="false" />
						</td>
					</tr>
					<tr>
						<td><label>休息时长：</label></td>
						<td><input id="releaseTime" name="releaseTime" class="mini-spinner" minValue="0" maxValue="10"
								increment="0.5" tooltip="小时" enabled="false" /></td>
					</tr>
					<tr>
						<td></td>
						<td>
							<a class="mini-button" iconCls="icon-save" onClick="saveTime()">保存</a>
						</td>
					</tr>
				</table>
			</div>
		</div>
	</div>

</body>

<script type="text/javascript">
	var date = new Date();
	var year = date.getFullYear();
	var YEARS = [];
	for (var i = year - 2; i < year + 3; i++) {
		YEARS.push({
			id: i,
			text: i
		});
	}

	miniInit();
	mini.get("year").setValue(year);

	var tree = mini.get("pageTree");

	// 加载浅蓝色背景和去除滚动条
	$('.mini-splitter').addClass('bg');
	//$('.fieldset-body-body').addClass('bg');

	tree.url = http.getUrl("/sys/depart/list.do");
	tree.load(tree.url);

	var editNode = null; // 当前编辑的节点
	/*********************************************************
	 * 左侧目录树的鼠标点击事件
	 *********************************************************/
	tree.on("nodedblclick", function (e) {
		// debugger
		editNode = e.node;
		tree.loadNode(editNode);
		if (editNode) {
			search();
		}
	});

	var userDatagrid = mini.get("userDatagrid");
	userDatagrid.set({
		url: http.getUrl("/user/page.do?status=1")
	});
	userDatagrid.on("rowdblclick", function (e) {
		var row = userDatagrid.getSelected();
		if (row) {
			var data = {
				"year": mini.get("year").getValue(),
				"userID": row.userID
			};
			http.post("/oa/calendar/user/list.do", data, function (page) {
				if (page.success) {
					var list = page.data;
					if (list.length > 0) {
						datagrid.setData(list);
					}
				} else {
					miniui.showInfoMessageBox(page.message);
				}
			});
		}
	});

	var calendarSort = mini.get("calendarSort");
	http.get("/oa/calendar/sort/list.do", function (page) {
		if (page.data.length > 0) {
			calendarSort.setData(page.data);
			calendarSort.on("valuechanged", function (e) {
				// console.log(e.selected);
				var data = e.selected;
				if (data.startWorkMinute == 0) {
					data.startWorkMinute = "00";
				}
				if (data.endWorkMinute == 0) {
					data.endWorkMinute = "00";
				}
				mini.get("startTime").setValue(data.startWorkHour + ":" + data.startWorkMinute);
				mini.get("endTime").setValue(data.endWorkHour + ":" + data.endWorkMinute);
				mini.get("releaseTime").setValue(data.releaseTime);
			});
		}
	});

	var datagrid = mini.get("datagrid1");
	datagrid.set({
		// url: http.getUrl("/com/bean/page.do")
		url: http.getUrl("/oa/calendar/list.do")
	});

	function search() {
		var data = {
			"year": mini.get("year").getValue()
		};
		var node = tree.getSelectedNode();
		if (node) {
			data["departID"] = node.departID;
		}
		http.post("/oa/calendar/list.do", data, function (page) {
			if (page.success) {
				var list = page.data;
				if (list.length > 0) {
					datagrid.setData(list);
				}
			} else {
				miniui.showInfoMessageBox(page.message);
			}
		});
		datagrid.load(data, null, miniui.showLoadErrorMessageBox);
		userDatagrid.load(data, null, miniui.showLoadErrorMessageBox);
	}

	datagrid.on("drawcell", function (e) {
		var record = e.record;
		var column = e.column;
		var field = e.field;
		var value = e.value;
		setColumnData(e, record[field], field);
	});

	// 单元格点击事件
	function setColumnData(e, data, week) {
		if (data.calendarDate != "") {
			var startWorkMinute = "" + data.startWorkMinute + "00";
			var endWorkMinute = "" + data.endWorkMinute + "00";
			startWorkMinute = startWorkMinute.substring(0, 2);
			endWorkMinute = endWorkMinute.substring(0, 2);
			e.cellStyle = "text-align:center";
			// color:#ff3333
			var html = '<div';
			// 工作日，假日，节日
			if (data.dayType == "假日") {
				html = html + ' style="color:#ff3333"';
			} else if (data.dayType == "节日") {
				html = html + ' style="color:#ff33ff"';
			}
			html = html + ">";
			html = html +
				'<div>' + data.calendarDate + '</div>' +
				/* '<div>上班时间:' + data.startWorkHour + ':' + startWorkMinute + '</div>' +
				'<div>下班时间:' + data.endWorkHour + ':' + endWorkMinute + '</div>' + */
				'<div>' + data.startWorkHour + ':' + startWorkMinute + " ~ " + data.endWorkHour + ':' + endWorkMinute + '</div>' +
				'<div>休息' + data.releaseTime + '小时</div>' +
				'<input type="checkbox" id ="' + data.calendarDate + '" name="' + data.calendarDate + '"/></div>';
			e.cellHtml = html;
		} else {
			e.cellHtml = "";
		}
	}

	datagrid.on("cellclick", function (e) {
		// var data = fieldGrid.getSelected();
		var record = e.record;
		var field = e.field;
		var date = record[field].calendarDate;
		var d = $("#" + date);
		var isChecked = d.prop('checked');
		d.prop('checked', !isChecked);
		debugger
	});

	var form1 = new mini.Form("#form1");

	function saveFestval() {
		var data = form1.getData(true);
		var node = tree.getSelectedNode();
		if (node) {
			data["departID"] = node.departID;
		} else {
			miniui.showInfoMessageBox("请先选择部门");
			return;
		}
		miniui.mask('数据处理中...');
		http.post("/oa/calendar/saveFestval.do", data, function (page) {
			if (page.success) {
				miniui.showInfoTip("保存成功");
				search();
			} else {
				miniui.showInfoMessageBox(page.message);
			}
			mini.unmask();
		});
	}

	var form2 = new mini.Form("#form2");

	function saveTime() {
		var data = form2.getData(true);
		if (data.startTime == "") {
			miniui.showInfoMessageBox("请填写上班时间");
			return;
		}
		if (data.endTime == "") {
			miniui.showInfoMessageBox("请填写下班时间");
			return;
		}
		if (data.releaseTime == "") {
			miniui.showInfoMessageBox("请填写休息时长");
			return;
		}
		if (data.startDate == "") {
			var allDates = [];
			var year = mini.get("year").getValue();
			var d = new Date(year + "-01-01");
			// console.log(common.dateToString(d));
			while (d.getFullYear() == year) {
				var month = d.getMonth() + 1;
				var day = d.getDate();
				month = "00" + month;
				day = "00" + day;
				month = month.substr(month.length - 2, 2);
				day = day.substr(day.length - 2, 2);
				day = year + '-' + month + '-' + day;
				var dc = $("#" + day);
				if (dc) {
					if (dc.prop('checked')) {
						allDates.push(day);
					}
				}
				d.setDate(d.getDate() + 1);
			}
			if (allDates.length == 0) {
				miniui.showInfoMessageBox("请先选择开始日期");
				return;
			}
			data["days"] = allDates;
		} else {
			if (data.endDate == "") {
				miniui.showInfoMessageBox("请先选择结束日期");
				return;
			}
		}
		// 设置在个人
		var users = userDatagrid.getSelecteds();
		if (users.length > 0) {
			var ids = common.getListKeys(users, "userID");
			data.userIDs = ids;
			miniui.mask('数据处理中...');
			http.post("/oa/calendar/user/set.do", data, function (page) {
				if (page.success) {
					miniui.showInfoTip("保存成功");
					// userDatagrid.doDblclick();
				} else {
					miniui.showInfoMessageBox(page.message);
				}
				mini.unmask();
			});
			return;
		}
		// 设置在部门
		var node = tree.getSelectedNode();
		if (node) {
			data["departID"] = node.departID;
			miniui.mask('数据处理中...');
			http.post("/oa/calendar/save.do", data, function (page) {
				if (page.success) {
					miniui.showInfoTip("保存成功");
					search();
				} else {
					miniui.showInfoMessageBox(page.message);
				}
				mini.unmask();
			});
			return;
		}
		miniui.showInfoMessageBox("请先选择个人或部门!");
	}

	function createCalendar() {
		var data = {
			"year": mini.get("year").getValue()
		};
		var node = tree.getSelectedNode();
		if (node) {
			data["departID"] = node.departID;
		}
		http.post("/oa/calendar/list.do", data, function (page) {
			if (page.success) {
				var list = page.data;
				if (list.length > 0) {
					datagrid.setData(list);
				} else {
					mini.confirm("指定部门的日历没有创建，是否要创建它?", "确定？",
						function (action) {
							if (action == "ok") {
								http.post("/oa/calendar/create.do", data, function (page) {
									if (page.success) {
										miniui.showInfoTip("创建成功!", "提示", null);
										search();
									} else {
										mini.alert(page.message, "提示", null);
									}
								});
							}
						}
					);
				}
			} else {
				miniui.showInfoMessageBox(page.message);
			}
		});
		datagrid.load(data, null, miniui.showLoadErrorMessageBox);
		userDatagrid.load(data, null, miniui.showLoadErrorMessageBox);
	}
</script>

</html>